<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Administrator_user extends MY_Administrator {
	function __construct() 
	{
		parent::__construct();
		//Load model
		$this->load->model('Administrator_user_model');
				
		/*Set Initial*/
		$this->dataContent = $this->data['admin_path'].preg_replace('/[\w]+_/','',$this->router->class).'/'.$this->router->method;
		$this->load->vars('Title', ucwords(preg_replace('/_/',' ',$this->router->method)));
		$this->load->vars('Url', base_url().$this->dataContent.'/');
	}

	public function ajax_valid_user()
	{
		$this->Administrator_user_model->ajax_valid_user();
	}

	public function ajax_valid_email()
	{
		$this->Administrator_user_model->ajax_valid_email();
	}
		
	public function user_profile($module=NULL)
	{
		/*POST*/
		if(isset($_POST) && !empty($_POST)):
			$this->Administrator_user_model->user_profile();
		endif;
		
		/*Load Js*/
		$this->data['js'][] = "zxcvbn.min.js";
		$this->data['js'][] = "user-profile.min.js";
		$this->data['js'][] = "password-strength-meter.min.js";
		$this->data['js'][] = "jquery.validate.js";
		$this->data['js'][] = "additional-methods.js";
		$this->data['dataUserInfo'] = $this->db->select('user.email,j.*')
		->join('user_info as j','j.user_id = user.user_id','INNER')
		->get_where('user',array('user.user_id' => $this->session->userdata('adminData')['user_id']),1)
		->row();
		
		/*Load Content*/
		$this->data['dataContent'] = $this->load->view($this->dataContent,$this->data,true);

		/*Load Templates*/
		$this->load->view($this->data['admin_path'].'templates/templates',$this->data);
	}	
	
	public function user_management($module=NULL,$arg=NULL)
	{
		switch($module):
			case "add":
				/*POST*/
				if(isset($_POST) && !empty($_POST)):
					$this->Administrator_user_model->user_management_add();
					redirect($this->dataContent);
				endif;
						
				/*Load Js*/
				$this->data['js'][] = "zxcvbn.min.js";
				$this->data['js'][] = "user-profile.min.js";
				$this->data['js'][] = "password-strength-meter.min.js";
				$this->data['js'][] = "jquery.validate.js";
				$this->data['js'][] = "additional-methods.js";
						
				/*Load Group*/
				$this->data['dataUserGroup'] = $this->db->select('group_id,group_name')->get('user_group')->result();	
				/*Load Content*/
				$this->data['dataContent'] = $this->load->view($this->dataContent.'_add',$this->data,true);
				/*Load Templates*/
				$this->load->view($this->data['admin_path'].'templates/templates',$this->data);
			break;
			case "edit":
				/*POST*/
				if(isset($_POST) && !empty($_POST)):
					$this->Administrator_user_model->user_management_add();
					redirect($this->dataContent);
				endif;
							
				/*Load Js*/
				$this->data['js'][] = "zxcvbn.min.js";
				$this->data['js'][] = "user-profile.min.js";
				$this->data['js'][] = "password-strength-meter.min.js";
				$this->data['js'][] = "jquery.validate.js";
				$this->data['js'][] = "additional-methods.js";
				
				$this->data['dataResult'] = $this->db
				->join('user_info as j','j.user_id = user.user_id','INNER')
				->get_where('user',array('user.user_id' => $arg),1)
				->row();
							
				/*Load Group*/
				$this->data['dataUserGroup'] = $this->db->select('group_id,group_name')->get('user_group')->result();
				/*Load Content*/
				$this->data['dataContent'] = $this->load->view($this->dataContent.'_edit',$this->data,true);
				/*Load Templates*/
				$this->load->view($this->data['admin_path'].'templates/templates',$this->data);				
			break;
			case "ajax_valid_user":
				$this->ajax_valid_user();
			break;			
			case "ajax_valid_email":
				$this->ajax_valid_email();
			break;
			default:
				/*Load Library*/
				$this->load->library('pagination');

				/*Search Condition*/
				$searchCondition = array('t.user_id IS NOT NULL');
				$group_id = $this->input->get('group_id');
				$text_search = $this->input->get('text_search');
				if(!empty($group_id)) $searchCondition[] = "j.group_id=".$group_id."";
				if(!empty($text_search)) $searchCondition[] = "t.username LIKE '%".$text_search."%' OR k.first_name LIKE '%".$text_search."%' OR k.last_name LIKE '%".$text_search."%' OR k.nickname LIKE '%".$text_search."%'";
				
				/*Order Condition*/
				$orderCondition = "t.user_id DESC";
				
				/*Pager*/		
				$config['base_url'] = current_url();
				$config['total_rows'] = $this->db->from('user as t')
				->join('user_group as j','j.group_id = t.group_id','INNER')
				->join('user_info as k','k.user_id = t.user_id','INNER')
				->where(''.implode(' AND ',$searchCondition).'')
				->count_all_results();
				$config['per_page'] = 20;
				$config['num_links'] = 10;
				$config['page_query_string'] = TRUE;
				$this->pagination->initialize($config);
				$this->data['pager'] = $this->pagination->create_links();
				$this->data['dataResult'] = $this->db->select('t.*,k.nickname,j.group_name')
				->join('user_group as j','j.group_id = t.group_id','INNER')
				->join('user_info as k','k.user_id = t.user_id','INNER')
				->order_by($orderCondition)
				->get_where('user as t',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))
				->result();
				
				/*Load Group*/
				$this->data['dataUserGroup'] = $this->db->select('group_id,group_name')->get('user_group')->result();
			
				/*Load Content*/
				$this->data['dataContent'] = $this->load->view($this->dataContent,$this->data,true);
				
				/*Load Templates*/
				$this->load->view($this->data['admin_path'].'templates/templates',$this->data);			
		endswitch;
	}	
}

/* End of file admin.php */
/* Location: ./application/controllers/admin.php */